package com.example.controller.frontend;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.common.ApiResult;
import com.example.entity.Course;
import com.example.entity.Major;
import com.example.entity.Unit;
import com.example.service.CourseService;
import com.example.service.MajorService;
import com.example.service.UnitService;
import com.example.vo.CourseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/fcourse")
@Api(tags = "课程管理")
public class FCourseController {
    @Resource
    private CourseService courseService;

    @Resource
    private MajorService majorService;

    @Resource
    private UnitService unitService;


    @ApiOperation("查询课程列表")
    @GetMapping("/findAll")
    public ApiResult findAll(){
        List<Course> list = courseService.list();
        return ApiResult.ok(list);
    }


    @ApiOperation("关联查询课程")
    @GetMapping("/selectVO")
    public ApiResult selectVO(){
        List<CourseVO> selectevo = courseService.selectevo();
        return ApiResult.ok(selectevo);
    }


    @ApiOperation("分页查询课程")
    @GetMapping("/findAllPage")
    public ApiResult findAllPage(@RequestParam(required = false,defaultValue = "") String courseName,
                                 @RequestParam(defaultValue = "1")Integer page,
                                 @RequestParam(defaultValue = "10") Integer size){
        Page<Course> itemPage = new Page<>(page, size);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("course_name",courseName);
        queryWrapper.orderByDesc("create_time");
        Page<Course> result = courseService.page(itemPage,queryWrapper);
        List<Course> records = result.getRecords();
            for (Course c:records){
                Major major = majorService.getById(c.getMajorId());
                Unit unit = unitService.getById(c.getUnitId());
                queryWrapper.eq("major_id",major);
                queryWrapper.eq("unit_id",unit);
                c.setMajor(major);
                c.setUnit(unit);
            }
        PageDTO<Course> pageDTO = new PageDTO<>();
        pageDTO.setTotal(result.getTotal());
        pageDTO.setRecords(result.getRecords());
        return ApiResult.ok(pageDTO);


    }





}
